package com.booth.base.controllers;

import java.util.List;

import com.booth.base.model.Groups;
import com.booth.base.utils.ResultStatus;
import com.jfinal.plugin.activerecord.Page;
/***
 * 组管理控制器
 * @TypeName：GroupsController
 * @description：
 * @author：Administrator
 */
public class GroupsController extends BaseController
{
	  /***
	   * 进入组管理列表页
	   */
	  public void index(){
	    render("groups.jsp");
	  }
	  /***
	   * 进入组管理添加页
	   */
	  public void au_page() {
	    Integer gid = getParaToInt("gid");
	    if ((gid != null) && (gid.intValue() > 0)){
	      Groups g = (Groups)Groups.dao.findById(gid);
	      setAttr("groups", g);
	    }
	    render("groups_au.jsp");
	  }

	  /***
	   * 获取列表页数据
	   */
	  public void list() {
	    int pageNum = getParaToInt("page", Integer.valueOf(1)).intValue();
	    int rows = getParaToInt("rows", Integer.valueOf(10)).intValue();
	    Page page = Groups.paginate(pageNum, rows);
	    setAttr("total", Integer.valueOf(page.getTotalRow()));
	    setAttr("rows", page.getList());
	    renderResult(ResultStatus.SUCCESS);
	  }

	  /***
	   * 保存组信息，判断id是否为空，为空则为新增，不为空则为修改
	   */
	  public void save() {
		    boolean success = false;
		    Groups g = (Groups)getModel(Groups.class);
		    Integer id = g.getInt("id");
		    if ((id == null) || (id.intValue() == 0))  {
		      success = g.save();
		    }
		    else{
		      success = g.update();
		    }
		    if (success)
		      renderResult(ResultStatus.SUCCESS);
		    else
		      renderResult(ResultStatus.ADD_FAILURL);
	  }

	  /***
	   * 删除组
	   */
	   public void delete(){
	    boolean success = false;
	    int id = getParaToInt("id", Integer.valueOf(0)).intValue();
	    if (id > 0){
	      success = new Groups().deleteById(Integer.valueOf(id));
	    }
	    if (success)
	      renderResult(ResultStatus.SUCCESS);
	    else
	      renderResult(ResultStatus.DELETE_FAILURE);
	  }
	   
	   /***
	   * 显示组的菜单
	   */
	  public void showPremissPage(){
	    int gid = getParaToInt("gid", Integer.valueOf(0)).intValue();
	    if (gid > 0){
	      setAttr("gid", Integer.valueOf(gid));
	      render("groups_premiss.jsp");
	    }
	    else{
	      renderResult(ResultStatus.SHOW_DATA_FAILURE);
	    }
	  }
	
	  /***
	   * 显示可用的组
	   */
	   public void showEnableGroups(){
	    boolean flag = getParaToBoolean("flag", Boolean.valueOf(false)).booleanValue();
	    List lst = Groups.showEnableGroups();
	    if (flag){
	      Groups g = new Groups();
	      g.set("group_name", "---无---");
	      lst.add(0, g);
	    }
	    renderJson(lst);
	  }
}